// 购物车模块
import axios from 'axios'
const cartModule = {
    namespaced: true,
    state: {
        gonglist: [],
        isAll: false
    },
    mutations: {
        getlist(state, newList) {
            state.gonglist = newList
        },
        blur(state) {
            state.isAll = !state.isAll;
            state.gonglist.forEach((item) => item.goods_state = state.isAll)
        },
        chenge(state, obj) {
            state.gonglist[obj.index].goods_state = obj.st
            state.isAll = state.gonglist.every((item) => item.goods_state)
        }
    },
    actions: {
        async getNewList(store) {
            let res = await axios({
                url: 'https://www.escook.cn/api/cart'
            })
            store.commit('getlist', res.data.list)
        }
    },
    getters: {
        totalCount(state) {
            return state.gonglist.reduce((sum, item) => {
                if (item.goods_state) {
                    sum += item.goods_count * 1
                }
                return sum
            }, 0)
        },
        totalPrice(state) {
            return state.gonglist.reduce((sum, item) => {
                if (item.goods_state) {
                    sum += item.goods_count * item.goods_price
                }
                return sum
            }, 0)
        },

    },
    modules: {}


}

export default cartModule